package io.coderyeah.ymcc.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import io.coderyeah.ymcc.domain.CourseChapter;
import io.coderyeah.ymcc.dto.CourseParamDTO;
import io.coderyeah.ymcc.service.ICourseService;
import io.coderyeah.ymcc.domain.Course;
import io.coderyeah.ymcc.query.CourseQuery;
import io.coderyeah.ymcc.result.JSONResult;
import io.coderyeah.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import io.coderyeah.ymcc.vo.CourseDetailVo;
import io.coderyeah.ymcc.vo.CourseInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.serializer.Serializer;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CourseParamDTO courseDTO) {
        return courseService.saveCourse(courseDTO);
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        Wrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name", query.getKeyword());
        page = courseService.selectPage(page, wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(), page.getRecords()));
    }

    /**
     * 上架课程
     */
    @PostMapping(value = "/onLineCourse/{id}")
    public JSONResult onLineCourse(@PathVariable Long id) {
        courseService.onLineCourse(id);
        return JSONResult.success();
    }

    /**
     * 下架课程
     */
    @PostMapping(value = "/offLineCourse/{id}")
    public JSONResult offLineCourse(@PathVariable Long id) {
        courseService.offLineCourse(id);
        return JSONResult.success();
    }


    /**
     * 课程详情
     */
    @GetMapping(value = "/detail/data/{id}")
    public JSONResult detail(@PathVariable Long id) {
        CourseDetailVo detailVo = courseService.courseDetailVo(id);
        return JSONResult.success(detailVo);
    }

    @GetMapping("/courseState/{id}")
    JSONResult getCourseStateById(@PathVariable("id") Long id) {
        return courseService.getCourseStateById(id);
    }


    @GetMapping("/info/{ids}")
    JSONResult getCourseInfo(@PathVariable("ids") List<Long> ids) {
        return courseService.getCourseInfo(ids);
    }

    @GetMapping("/killInfo/{orderNo}")
    JSONResult killInfo(@PathVariable("orderNo") String orderNo) {
        CourseInfoVo courseInfoVo = courseService.courseOrderKillInfo(orderNo);
        return JSONResult.success(courseInfoVo);
    }
}
